<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" /><!--iso-8859-1-->
        <!--<meta charset="windows-1250" />-->
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <?php
               global $USER, $CFG, $COURSE;
               require_once('../config.php');  
             
                if (!isloggedin()) {
                    header( 'Location: login.php' ) ;                      
                }
        ?>
        <title>Bienvenido</title>
        <!--<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />-->
        <!--<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>-->
        <!--<script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>-->
        <link rel="stylesheet" href="css/jquery.mobile-1.0.1.css" />
        <link rel="stylesheet" href="css/style.css" />       
        <script src="scripts/jquery.min1.6.4.js"></script>        
        <script src="scripts/jquery.mobile-1.0.1.min.js"></script>
        <script src="scripts/utpljs.js"></script>
    </head>
    <body>
        
        <div id="content">
        <?php
           
        ?>
        
        <div data-role="page" id="muro"  >
            <div data-theme="b" data-position="inline" data-role="header">               
                <h1>
                    EVA
                </h1>
                <a href="./login.php" class="ui-btn-right"  data-role="button" data-icon="delete" data-transition="fade" data-iconpos="notext">Salir</a>               
            </div>
            
            <div  data-role="content" >
                <?php
                     
                      $mis_cursos = get_records_sql("select courseid as id, c.fullname from mdl_enrol_utpl as e, mdl_course as c where e.courseid = c.id and  pdoid = 208 and userid = $USER->id"); 
                        $list = array();

                        foreach($mis_cursos as $curso){
                            $list[] = $curso->id;
                        }
                       
                       $mensajes= get_messages_total($USER->id,$list);
                     echo '<ul data-role="listview" data-inset="true" data-count-theme="a">                             
                             <li data-role="list-divider">Bienvenido</p></li>
                             
                                    <div style="width: 15%" class="ui-btn-left">
                                        <img src="'.$CFG->wwwroot.'/user/pix.php?file='. $USER->id.'/f1.jpg"  alt="Estudiante picture"/>
                                    </div>
                                    <div style="width: 75%" class="ui-btn-right">
                                        <strong>'.$USER->firstname . " " . $USER->lastname.'</strong>
                                        <h2>'.$USER->city.'</h2>
                                    </div>              
                             <li><a href="#"><img height="25px" width="25px" src="imgs/user_info.png" alt="Info User" class="ui-li-icon">Perfil</a></li> 
                             <li><a data-transition="slide" href="#smslist"><img height="25px" width="25px" src="imgs/mail.png" alt="Info User" class="ui-li-icon">  Mensajes<span class="ui-li-count">'.count($mensajes)  .'</span></a></li> 
                             <li><a data-transition="slide" href="#courselist"><img height="25px" width="25px" src="imgs/course_icon.png" alt="Info User" class="ui-li-icon">Cursos<span class="ui-li-count">'. count( $list ) .'</span></a>
                             </li>
                 </ul>';
                 ?>
             
            </div>
            
            <div data-theme="b" data-role="footer">
                <h5>
                     <img src="./imgs/logo2.png" whith="50px" height="40px"   alt="logo utpl"/>
                </h5>
            </div>
       </div>
        
      
        
        <?php
        
 
           
        ?>
        
        <div  data-role="page"  id="courselist" >
            <div data-theme="b" data-role="header">
                <a href="#muro" class="muro" data-role="button" data-icon="home" data-transition="fade" data-iconpos="notext">Home</a>               
                <h1>
                    Mis Cursos <span class="ui-li-count"><?php echo count($mis_cursos); ?></span>
                </h1>
                <a href="login.php" class="salir" data-role="button" data-icon="delete" data-transition="fade" data-iconpos="notext">Salir</a>               
            </div>
            
            <div  data-role="content">
                <ul data-role="listview">
                                             
                <?php                                           
                          foreach( $mis_cursos as $curso ){
                              $count = get_record_sql ("SELECT
                                                        Count(cs.id) as no_anun
                                                        FROM
                                                        {$CFG->prefix}course_sections AS cs
                                                        WHERE
                                                        cs.visible = 1 AND
                                                        cs.summary <> '' AND
                                                        cs.course = $curso->id");

                                 $rusers = get_teachers( $curso->id );
                                 
                                if( $rusers ){
                                    $datos_nm ="";

                                    foreach($rusers as $profe){
                                        if( $profe["name"] != "nn"){

                                            $datos_nm .= "<p>". strtoupper(utf8_encode($profe["name"] )) . "</p> ";                                                 
                                        }
                                    }
                                }//id="curso_'.$curso->id.'" class="courseview"
                                 //echo '<li><a id="curso_'.$curso->id.'" class="courseview" rel="external" href="course/course.php?courseid='.$curso->id.'"><h3>'.$curso->fullname.'</h3> <p>'.$datos_nm.'</p></a><span class="ui-li-count">anuncios: '.$count->no_anun.'</span>';
                                 echo '<li><a id="curso_'.$curso->id.'" class="courseview"  href=""><h3>'.$curso->fullname.'</h3> <p>'.$datos_nm.'</p></a><span class="ui-li-count">anuncios: '.$count->no_anun.'</span>';
                               //echo '<li><a id="curso_'.$curso->id.'" class="courseview"  href=""><h3>'.$curso->fullname.'</h3> <p>'.$datos_nm.'</p></a><span class="ui-li-count">anuncios: '.$count->no_anun.'</span>';

                          }
                ?>
                </ul>
                
            </div>
            
            <div data-theme="b" data-role="footer">
                <h5>
                    <img src="./imgs/logo2.png" whith="50px" height="40px"   alt="logo utpl"/>
                </h5>
            </div>
       </div>
        
         <?php
           
        ?>
        
        <div data-role="page" id="smslist" >
            <div data-theme="b" data-role="header">               
                <a href="#muro" class="muro" data-role="button" data-icon="home" data-transition="fade" data-iconpos="notext">Home</a>               
                <h1>
                    Mensajes
                </h1>
                <a href="login.php" class="salir" data-role="button" data-icon="delete" data-transition="fade" data-iconpos="notext">Salir</a>               

            </div>
            
            <div  data-role="content">
                <?php
                    echo '<div data-role="collapsible-set">';
                     $mis_cursos[1]->id = 1;
                     $mis_cursos[1]->fullname = 'OTROS';
                    foreach ($mis_cursos as $curso){
                             $mensajes = get_messages($USER->id,$curso->id);      
                                                        
                             if($mensajes){
                                 echo '<div data-role="collapsible" data-theme="e" data-content-theme="b">
                                        <h3>'.$curso->fullname.'  (<span class="ui-li-count">'.count($mensajes).'</span>)</h3>
                                        <ul data-role="listview" data-inset="true" data-count-theme="e">';
                                 
                                        foreach( $mensajes as $mensaje){                                         
                                             echo '<li> 
                                                       <img src="'.$CFG->wwwroot.'/user/pix.php?file='.$mensaje->id.'/f1.jpg">
                                                       <p>'. $mensaje->firstname .' '. $mensaje->lastname .'</p> 
                                                       <span onclick="leer(this);" class="ui-li-count">1 day ago</span> 
                                                   </li>
                                                     
                                               ';
                                        }                                          
                                 echo ' </ul>
                                            </div>';
                             }else{
                                 echo '<div data-role="collapsible">
                                        <h3>'.$curso->fullname.' (<span class="ui-li-count">0</span>)</h3>
                                        <p>Sin mensajes</p>
                                        </div>';
                             }                 
                    }
                    echo "</div>";
                 ?>
            </div>
            
            <div data-theme="b" data-role="footer">
                <h5>
                    <img src="./imgs/logo2.png" whith="50px" height="40px"   alt="logo utpl"/>
                </h5>
            </div>
       </div>
            
            <?php
            
            ?>
            <div data-role="page" id="sd" >
            <div data-theme="b" data-role="header">               
                <a href="#muro" class="muro" data-role="button" data-icon="home" data-transition="fade" data-iconpos="notext">Home</a>               
                <h1>
                    Mensajes
                </h1>
                <a href="login.php" class="salir" data-role="button" data-icon="delete" data-transition="fade" data-iconpos="notext">Salir</a>               

            </div>
            
            <div  data-role="content" id="coursemen">
                
            </div>
            
            <div data-theme="b" data-role="footer">
                <h5>
                    <img src="./imgs/logo2.png" whith="50px" height="40px"   alt="logo utpl"/>
                </h5>
            </div>
       </div>
            
            
        <?php
        
function get_teachers($curso){
    
   $managerroles = 3;
    $context = get_context_instance(CONTEXT_COURSE, $curso);
    $canseehidden =  has_capability('moodle/role:viewhiddenassigns', $context);

    $rusers = get_role_users($managerroles, $context,true, '', 'r.sortorder ASC, u.lastname ASC', $canseehidden);
    if( $rusers ){
        $datos_nm =array();

        foreach($rusers as $profe){
            if( $profe->firstname != "nn"){

                $datos_nm [] = array("id" => $profe->id ,"name" => strtoupper(utf8_decode($profe->firstname . " " .$profe->lastname)) , "mail" => $profe->email , "ciudad" => $profe->city ) ;                                                 
            }
        }
    }
    
    return $datos_nm;
} //end function

function get_messages_total($receptor, $cursos){
    /*
     * Obtiene los mensajes por curso
     */
 
    $users = get_records_sql("SELECT r.id as id_sms,u.id, u.firstname, u.lastname, u.picture, u.imagealt, u.lastaccess,r.timecreated ,
                                                count(r.id) as messagecount, timecreated, ma.courseid
                                                FROM mdl_message r, mdl_message_answered ma, mdl_user u

                                                WHERE u.id = r.useridfrom 
                                                        AND ma.messageid = r.id 

                                                        AND r.useridto = {$receptor}
                                                        AND ma.messageanswerid = 0  AND ma.courseid in (1,2," .implode(",",$cursos).")
                                                GROUP BY u.id, u.firstname, u.lastname, u.picture, u.imagealt, u.lastaccess

                                                ORDER BY u.firstname ASC");
                                                        
   if($users){
       return $users;
       
   }else{
       return array();
   }
    
    
}//end get_messages

function get_messages($receptor, $curso){
    /*
     * Obtiene los mensajes por curso
     */

    
    $users = get_records_sql("SELECT r.id as id_sms,u.id, u.firstname, u.lastname, u.picture, u.imagealt, u.lastaccess,r.timecreated ,
                                                count(r.id) as messagecount, timecreated, ma.courseid
                                                FROM mdl_message r, mdl_message_answered ma, mdl_user u

                                                WHERE u.id = r.useridfrom 
                                                        AND ma.messageid = r.id 

                                                        AND r.useridto = {$receptor}
                                                        AND ma.messageanswerid = 0  AND ma.courseid = $curso
                                                GROUP BY u.id, u.firstname, u.lastname, u.picture, u.imagealt, u.lastaccess

                                                ORDER BY u.firstname ASC");
                                                        
   if($users){
       return $users;
       
   }else{
       return array();
   }
    
}//end get_messages
        ?>
        </div>
        
    </body>
</html>
